package com.example.server.controller;

import com.example.server.entity.Product;
import com.example.server.entity.User;
import com.example.server.service.UserService;
import com.example.server.util.Util;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

//@Slf4j
@Controller
@RequestMapping("user")
@CrossOrigin("http://localhost:5173")
public class UserController extends BaseController{

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("queryAll")
    @ResponseBody
    private List<User> queryAll(){
        return userService.queryAll();
    }

    @PostMapping("create")
    @ResponseBody
    private boolean create(@RequestPart MultipartFile file, @RequestPart User user){

        System.out.println(file.getOriginalFilename() + file.getSize());
        user.setAvatar(Util.savePicture(file, "D:/softwear/shixun/project-team06/photo/avatar"));
        userService.create(user);
        return true;
    }

    @PostMapping("queryByAccountAndPassword")
    @ResponseBody
    private User queryByAccountAndPassword(@RequestBody Map<String, String> map) {
        User user = userService.queryOne("queryByAccountAndPassword", map);
        session.setAttribute("user",user);
        return user;
    }

    @GetMapping("queryById")
    @ResponseBody
    private User queryById(@RequestParam BigInteger uid){
        return userService.queryOne("queryById",uid);
    }

    @PutMapping("modify")
    @ResponseBody
    private boolean modify(@RequestPart(required = false) MultipartFile file, @RequestPart User user) {
        System.out.println(file);
        if (file != null) {
            user.setAvatar(Util.savePicture(file, "D:/softwear/shixun/project-team06/photo/avatar"));
        }
        userService.modify(user);
        return true;
    }

}
